package com.example.demo.controller;

import com.example.demo.common.Result;
import com.example.demo.entity.PersonEvent;
import com.example.demo.service.PersonEventService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/personEvent")
public class PersonEventController {

    @Resource
    private PersonEventService personEventService;

    // 新增事件
    @PostMapping("/add")
    public Result add(@RequestBody PersonEvent event) {
        personEventService.add(event);
        return Result.success();
    }

    // 更新事件
    @PutMapping("/update")
    public Result update(@RequestBody PersonEvent event) {
        personEventService.update(event);
        return Result.success();
    }

    // 删除事件
    @DeleteMapping("/deleteById/{eventId}")
    public Result deleteById(@PathVariable Long eventId) {
        personEventService.deleteById(eventId);
        return Result.success();
    }

    // 查询单个事件
    @GetMapping("/selectById/{eventId}")
    public Result selectById(@PathVariable Long eventId) {
        PersonEvent event = personEventService.selectById(eventId);
        return Result.success(event);
    }

    // 查询所有事件
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<PersonEvent> list = personEventService.selectAll();
        return Result.success(list);
    }

    // 根据人员ID查询事件
    @GetMapping("/selectByPersonId/{personId}")
    public Result selectByPersonId(@PathVariable Long personId) {
        List<PersonEvent> list = personEventService.selectByPersonId(personId);
        return Result.success(list);
    }

    // 分页查询
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<PersonEvent> pageInfo = personEventService.selectPage(pageNum, pageSize);
        return Result.success(pageInfo);
    }
}